package com.flight.trans.activity;

import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.flight.trans.R;
import com.flight.trans.base.BaseActivity;
import com.flight.trans.bean.TugBean.TugData;
import com.flight.trans.bean.NfcBean;
import com.flight.trans.bean.NfcOldBean;
import com.flight.trans.global.Constant;
import com.flight.trans.http.NetworkUtils;
import com.flight.trans.http.network.OkGoBackListener;
import com.flight.trans.util.CommonUtils;
import com.flight.trans.util.JsonUtils;
import com.flight.trans.util.NfcUtils;
import com.flight.trans.util.voice.SpeakUti;

import java.util.List;

/**
 * NFC扫描
 */
public class TugNFCActivity extends BaseActivity {

    private RelativeLayout mRlContainer ;
    private TextView mTvScanTitle;
    private NfcUtils mNfcUtils;

    private List<TugData> mContainerDataList ;

    @Override
    protected int setContentView() {
        return R.layout.activity_nfc;
    }

    @Override
    protected void initView() {
        mRlContainer = findViewById(R.id.mRlContainer);
        mTvScanTitle = findViewById(R.id.mTvScanTitle);
    }

    @Override
    protected void initData() {
        mTvScanTitle.setText("获取拖斗信息中，请稍后...");

        // 初始化NFC功能
        mNfcUtils = new NfcUtils(mActivity);
    }

    @Override
    protected void initConnect() {
        NetworkUtils.getTugList(new OkGoBackListener(mActivity){
            @Override
            public void onSuccess(Object body) {
                mContainerDataList = (List<TugData>) body;

                if(mContainerDataList != null && mContainerDataList.size() != 0) {
                    Message message = Message.obtain();
                    message.obj = "仅支持对旧拖斗标签覆盖写入，不支持其他标签的写入操作";
                    mHandler.sendMessage(message);
                }
            }
        } , 0);
    }

    private Handler mHandler = new Handler(){
        @Override
        public void handleMessage(@NonNull Message msg) {
            mTvScanTitle.setText(msg.obj.toString());
        }
    };

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);

        String fromTag = mNfcUtils.readNFCFromTag(intent);
        NfcOldBean nfcOldBean = JsonUtils.parseJsonToBean(fromTag, NfcOldBean.class);

        if(nfcOldBean != null) {
            if (!TextUtils.isEmpty(getNfcBean(nfcOldBean))) {
                String msg = mNfcUtils.writeNFCToTag(getNfcBean(nfcOldBean), intent) ? "写入成功" : "写入失败";

                sendMessage(getNfcBean(nfcOldBean));

                SpeakUti.getInstance().play(msg);
            } else {
                SpeakUti.getInstance().play("写入失败");
                sendMessage("系统内没有当前读取到的拖斗号\n"+fromTag);
            }
        }else {
            SpeakUti.getInstance().play("写入失败");
            sendMessage("读取到的标签内容错误\n"+fromTag);
        }
    }

    private String getNfcBean(NfcOldBean nfcOldBean){
        if(mContainerDataList == null || mContainerDataList.size() == 0)
            return null;

        for (int i = 0; i < mContainerDataList.size(); i++) {
            if(mContainerDataList.get(i).getCode().equals(nfcOldBean.getDeviceCode())){
                return JsonUtils.parseBeanToJson(new NfcBean(mContainerDataList.get(i).getId() , mContainerDataList.get(i).getCode() ,Constant.Port_Tug));
            }
        }

        return null;
    }

    /**
     * 点击事件
     * @param view
     */
    public void onClick(View view){
        if(CommonUtils.isFastDoubleClick())
            return;

        switch (view.getId()){
            case R.id.mBtnCancelScan :
                // 取消扫描
                onFinish();
                break;
        }
    }

    private void sendMessage(String value){
        Message message = Message.obtain();
        message.obj = value;
        mHandler.sendMessage(message);
    }

    @Override
    protected void onResume() {
        super.onResume();
        //开启前台调度系统
        if(mNfcUtils.NfcCheck(mActivity)) {
            mNfcUtils.mNfcAdapter.enableForegroundDispatch(this, NfcUtils.mPendingIntent, NfcUtils.mIntentFilter, NfcUtils.mTechList);
        }else {
            onFinish();
        }
    }
    @Override
    protected void onPause() {
        super.onPause();
        //关闭前台调度系统
        if(mNfcUtils.NfcCheck(mActivity)) {
            mNfcUtils.mNfcAdapter.disableForegroundDispatch(this);
        }else {
            onFinish();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mNfcUtils = null;
    }
}
